package mybatis.service.impl;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;


import mybatis.entity.ComputerAccessories;
import mybatis.entity.Game;
import mybatis.entity.Supplyer;

import mybatis.util.CsvUtil;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;


public class CSVhandle {
	public static <GamesService> List<Game> transform() throws Exception {
		List<String[]> list = CsvUtil.readCSV("gamedetailssave.csv");
		List<Game> listGame = new ArrayList<>();
		int i = 1;
		for(String[] strs :list) {
			Game game = new Game();
			//1列 游戏编号
			game.setGame_number(Integer.parseInt(strs[0]));
			//2列 游戏名字
			game.setGame_name(strs[1]);
			//3列 游戏封面
			game.setGame_src(strs[2]);
			//4列 游戏折扣
			game.setGame_discount(strs[3]);
			//5列 游戏原价
			game.setGame_original_price(new BigDecimal(strs[4]));
			//6列 游戏现价
			game.setGame_final_price(new BigDecimal(strs[5]));
			//7列 游戏链接
			game.setGame_url(strs[6]);
			//8列 获取所有图片链接
//			game.setDetail_srcs(getSrcs(strs[7]));
//			System.out.println(Arrays.toString(getSrcs(strs[7])) );
			//9列 游戏logo
			game.setLogo_src(strs[8]);
			//第10列 游戏厂商
			game.setSupplyer(getSupplyer(strs[9]));
//			System.out.println(i+""+getSupplyer(strs[9]));
			//第11列 最低配置
//			System.out.println(strs[10]);
			game.setComputer_low(getComputer(strs[10]));
//			System.out.println(i+""+getComputer(strs[10]));
			//第12列 最高配置
			game.setComputer_high(getComputer(strs[11]));
//			System.out.println(i+""+getComputer(strs[11]));
			//第13列 语音
			game.setGame_audio(spilit(strs[12]));
//			System.out.println(i+""+Arrays.toString(spilit(strs[12])) );
			//14列 文本
			game.setGame_langueages(spilit(strs[13]));
			
//			System.out.println(i+""+spilit(strs[13]).toString() );
			//15列 版权信息
//			String s = strs[14];
			game.setGame_privacy(strs[14]);
//			System.out.println(s);
			//16列 游戏类型
			game.setGame_types(spilit(strs[15]));
//			System.out.println(i+""+spilit(strs[15]).toString() );
			//17列 游戏功能
			game.setGame_functions(spilit(strs[16]));
//			System.out.println(i+""+spilit(strs[16]).toString() );
			//18列 游戏介绍
			game.setGame_texts(strs[17]);
			listGame.add(game);
		}
//		TreeSet<String> set = new TreeSet<>((s1,s2) -> ((String)s1).compareTo((String)s2));
//		for(Game g :listGame) {
//			g.getGame_types();
//			for(String s : g.getGame_langueages()) {
//				if(s != null && !s.equals(null) && !s.equals(" "))set.add(s);
//			}
//			for(String s2 : g.getGame_audio()) {
//				if(s2 != null && !s2.equals(null) && !s2.equals(" "))set.add(s2);
//			}
//		}
//		List<String> list2 = new ArrayList<>();
//		System.out.println(set);
//		for(String t : set) {
////			System.out.println(t);
//			list2.add(t);
////			SqlSession ss = DBUtils.getSqlSession();
////			TestDao dao = ss.getMapper(TestDao.class);
////			dao.add(t);
////			ss.commit();
////			ss.close();
//		}
//		list2.remove(0);
//		System.out.println(list2);
		GamesService gservice = (GamesService) new GamesServiceImpl();
//		TypeService fService = new TypeServiceImpl();
		LanguageServiceImpl lService = new LanguageServiceImpl();
		for(Game t : listGame) {
			
//			int number = t.getGame_number();
//			Game game = gservice.findByNumber(number);
//			int gid = game.getId();
//			int lid,vid;
//			{
//				List<String> features = t.getGame_types();
//				TreeSet<String> typeSet = new TreeSet<>((s1,s2) -> ((String)s1).compareTo((String)s2));
//				for(String s:features) {
//					typeSet.add(s);
//				}
//				for(String types : typeSet) {
//					System.out.println(types);
//					if(types.isEmpty()) continue;
//					lid = fService.findId(types);
//					System.out.println(lid);
//					gservice.addGameType(gid, lid);;
//				}
//			}
//			{
//				List<String> langueages = t.getGame_langueages();
//				TreeSet<String> languagaSet = new TreeSet<>((s1,s2) -> ((String)s1).compareTo((String)s2));
//				for(String s:langueages) {
//					languagaSet.add(s);
//				}
//				for(String languages:languagaSet) {
//					System.out.println(languages);
//					if(languages.isEmpty()) continue;
//					lid = lService.findByName(languages);
//					System.out.println(lid);
//					gservice.addGameLanguage(gid, lid);
//				}
//			}
//			{
//				List<String> langueages = t.getGame_audio();
//				TreeSet<String> audioSet = new TreeSet<>((s1,s2) -> ((String)s1).compareTo((String)s2));
//				for(String s:langueages) {
//					audioSet.add(s);
//				}
//				for(String audio:audioSet) {
//					System.out.println(audio);
//					if(audio.isEmpty()) continue;
//					vid = lService.findByName(audio);
//					System.out.println(vid);
//					gservice.addGameVoice(gid, vid);
//				}
//			}
//			System.out.println(id)languagaSet
		}
		return listGame;
	}
//	public static List<String> getSrcs(String old) {
//		return old.replaceAll("\\[", "").replaceAll("\\]", "").replaceAll("'", "").replaceAll(" ", "").split(",");
//	}
	public static Supplyer getSupplyer(String old) throws JsonParseException, JsonMappingException, IOException {
		if(old.equals(null) || old.equals("")) {
			return null;
		}
		ObjectMapper om = new ObjectMapper();
		Supplyer sup = om.readValue(old, Supplyer.class);
		return sup;
	}
	public static ComputerAccessories getComputer(String old) throws JsonParseException, JsonMappingException, IOException {
		if(old.equals(null) || old.equals("")) {
			System.out.println(123213);
			return null;	
		} 
		ObjectMapper om = new ObjectMapper();
		ComputerAccessories comput = om.readValue(old,ComputerAccessories.class);
		return comput;
	}
	public static List<String> spilit(String old) {
		String[] strs = old.split(",");
		List<String> strsNew = new ArrayList<>();
		for(int i = 0;i <strs.length ;i++) {
			strsNew.add(strs[i].trim());
		}
		return strsNew;
		
	}
	public static void main(String[] args) throws Exception {
//		List<Game> list = transform();
//		GamesService gservice = new GamesServiceImpl();
//		for(Game g : list) {
//			List<String> strs = g.getDetail_srcs();
////			System.out.println(Arrays.toString(strs));
//			int number = g.getGame_number();
//			Game game2 = gservice.findByNumber(number);
//			int gid = game2.getId();
//			if(strs.size()>0) {
//				for(String str: strs) {
//					gservice.addSrcs(str, gid);
//				}	
//			}
//			List<String> langueages = t.getGame_langueages();
//			TreeSet<String> languagaSet = new TreeSet<>((s1,s2) -> ((String)s1).compareTo((String)s2));
//			for(String s:langueages) {
//				languagaSet.add(s);
//			}
//			for(String languages:languagaSet) {
//				System.out.println(languages);
//				if(languages.isEmpty()) continue;
//				lid = lService.findByName(languages);
//				System.out.println(lid);
//				gservice.addGameLanguage(gid, lid);
//			}
//		}
	}
	
}
